package com.novunda.license.core;

import java.beans.*;
import java.io.*;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;

/**
 * 存储服务
 *
 * @author linfeng
 * @since 2016/10/12.
 */
final class PersistenceService implements Serializable {

    private static final long serialVersionUID = 5002368743335014309L;

    private static final Map<Class<?>, PersistenceDelegate> persistenceDelegateMap = new HashMap<Class<?>, PersistenceDelegate>();
    public static final int BUF_SIZE = 8192;

    private PersistenceService() {
    }

    private static ExceptionListener createExceptionListener() {
        return new ExceptionListener() {
            @Override
            public void exceptionThrown(Exception var1) {
                throw var1 instanceof UndeclaredThrowableException ? (UndeclaredThrowableException) var1 : new UndeclaredThrowableException(var1);
            }
        };
    }

    public static synchronized void setPersistenceDelegate(Class<?> var0, PersistenceDelegate var1) {
        persistenceDelegateMap.put(var0, var1);
    }

    private static synchronized void installPersistenceDelegates(Encoder var0) {

        for (Map.Entry<Class<?>, PersistenceDelegate> entry : persistenceDelegateMap.entrySet()) {

            var0.setPersistenceDelegate(entry.getKey(), entry.getValue());
        }

    }

    public static void store(Object var0, OutputStream var1) throws LicenseException {
        if (var1 == null) {
            throw new NullPointerException("OutputStream is null");
        } else {
            try {
                BufferedOutputStream var2 = null;
                XMLEncoder var3 = null;

                try {
                    var2 = new BufferedOutputStream(var1, BUF_SIZE);
                    var3 = new XMLEncoder(var2);
                    installPersistenceDelegates(var3);
                    var3.setExceptionListener(createExceptionListener());
                    var3.writeObject(var0);
                } finally {
                    if (var3 != null) {
                        try {
                            var3.close();
                        } catch (Exception var12) {
                            var2.close();
                            throw var12;
                        }
                    } else if (var2 != null) {
                        var2.close();
                    } else {
                        var1.close();
                    }

                }

            } catch (UndeclaredThrowableException var14) {
                throw new LicenseException(var14.getCause());
            } catch (Exception var15) {
                throw new LicenseException(var15);
            }
        }
    }

    public static void store(Object var0, File var1) throws LicenseException {
        if (var1 == null) {
            throw new NullPointerException();
        } else {
            File var2 = null;
            boolean var3 = false;

            try {
                var2 = getRenamedFile(var1);
                var3 = var1.renameTo(var2);
                store(var0, new FileOutputStream(var1));
                if (var3) {
                    var2.delete();
                }

            } catch (Exception var8) {
                Exception var4 = var8;
                if (var3) {
                    try {
                        var1.delete();
                    } catch (Exception var7) {
                        var4 = var7;
                    }

                    try {
                        var2.renameTo(var1);
                    } catch (Exception var6) {
                        var4 = var6;
                    }
                }

                throw var4 instanceof LicenseException ? (LicenseException) var4 : new LicenseException(var4);
            }
        }
    }

    private static File getRenamedFile(File var0) {
        StringBuilder var1 = new StringBuilder(var0.getPath());

        File var2;
        do {
            var2 = new File(var1.append('~').toString());
        } while (var2.exists());

        return var2;
    }

    public static byte[] store2ByteArray(Object var0) throws LicenseException {
        try {
            ByteArrayOutputStream var1 = new ByteArrayOutputStream();
            store(var0, var1);
            return var1.toByteArray();
        } catch (Exception var3) {
            throw new LicenseException(var3);
        }
    }

    public static String store2String(Object var0) throws LicenseException {
        try {
            ByteArrayOutputStream var1 = new ByteArrayOutputStream();
            store(var0, var1);
            return var1.toString("UTF-8");
        } catch (UnsupportedEncodingException var2) {
            throw new AssertionError(var2);
        } catch (Exception var4) {
            throw new LicenseException(var4);
        }
    }

    public static Object load(InputStream var0) throws LicenseException {
        if (var0 == null) {
            throw new NullPointerException();
        } else {
            XMLDecoder var1 = null;

            Object var2;
            try {
                var1 = new XMLDecoder(new BufferedInputStream(var0, BUF_SIZE), null, createExceptionListener());
                var2 = var1.readObject();
            } catch (UndeclaredThrowableException var12) {
                throw new LicenseException(var12.getCause());
            } catch (Exception var13) {
                throw new LicenseException(var13);
            } finally {
                if (var1 != null) {
                    try {
                        var1.close();
                    } catch (Exception var11) {
                        throw new LicenseException(var11);
                    }
                }

            }

            return var2;
        }
    }

    public static Object load(File var0) throws LicenseException {
        if (var0 == null) {
            throw new NullPointerException();
        } else {
            try {
                return load(new FileInputStream(var0));
            } catch (Exception var3) {
                throw new LicenseException(var3);
            }
        }
    }

    public static Object load(byte... var0) throws LicenseException {
        if (var0 == null) {
            throw new NullPointerException();
        } else {
            try {
                return load(new ByteArrayInputStream(var0));
            } catch (LicenseException var2) {
                throw var2;
            } catch (Exception var3) {
                throw new LicenseException(var3);
            }
        }
    }

    public static Object load(String var0) throws LicenseException {
        if (var0 == null) {
            throw new NullPointerException();
        } else {
            try {
                return load(var0.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException var2) {
                throw new AssertionError(var2);
            }
        }
    }

}
